package com.hctforgreen.greenservice.machineManager.impl;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.RefrigerantCalculatorActivity;
import com.hctforgreen.greenservice.machineManager.MachineBase;
import com.hctforgreen.greenservice.model.LocationEntity;
import com.hctforgreen.greenservice.model.MutipleMachineEntity;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.ui.adapter.SpinnerStringsAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.Utils;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Parcel;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.Serializable;

/**
 * 多联机
 * @author 390694
 *
 */
public class MutipleMachine extends MachineBase{
	
	private Context context;
	public MutipleMachineEntity mutipleMachineEntity;//2017-1-11 多联机特殊属性 ,会在机型提示中使用到
	private View Layout_MutipleMachine;
	private TextView edit_lengmei_add;
	private Button button_lengmei_add;
	private TextView edit_out_totle_line;
	private Spinner spinner_installation_site;
	private Spinner spinner_construction_area;
	private Spinner spinner_in_out_length;
	private Spinner spinner_in_out_lc_type;
	private Spinner spinner_in_in_lc;
	private Spinner spinner_out_max_length_type;
	private Spinner spinner_in_out_value;
	private Spinner spinner_out_max_length_value;
	
	/**
	 * // 多联机：冷媒追加量；热水机：热水用水点;
	 */
	private String addCoolantWeight;
	/**
	 * // 多联机：外机总线径；热水机：户型面积
	 */
	private String diameter;
	/**
	 * // 多联机：内机到外机连接管长度；热水机：水箱安装位置 ||商用热水机：机组安装位置
	 */
	private String ioConnectionLength;
	/**
	 * // 多联机：内机与外机的最大落差；热水机：冷媒连接管||商用热水机：机组与水箱落差
	 */
	private String dropHeight;
	/**
	 * // 多联机：内机与内机的最大落差；热水机：使用水源;商用热水机：使用水源
	 */
	private String inDropHeight;
	/**
	 * // 安装位置
	 */
	private String installationSite;
	/**
	 * 商用热水机：是否安装回水装置// 多联机：外机最大风管长度；热水机：使用人数
	 */
	private String tubeLength;
	/**
	 * // 建筑面积
	 */
	private String constructionArea;

	/**
	 * 初始化
	 * @param context
	 */
	public MutipleMachine(Activity context) {
		super(context);
		this.context = context;
		//390697 屏蔽是为了在机型提示的关联资料中去获取开机密码
//		initView();
		mutipleMachineEntity = new MutipleMachineEntity();
	}

	@Override
	public void initView() {
		Layout_MutipleMachine = findViewById(R.id.Layout_MutipleMachine);
		edit_lengmei_add = (TextView) findViewById(R.id.edit_lengmei_add);
		button_lengmei_add = (Button) findViewById(R.id.button_lengmei_add);
		edit_out_totle_line = (TextView) findViewById(R.id.edit_out_totle_line);
		
		spinner_installation_site = (Spinner) findViewById(R.id.spinner_installation_site);
		spinner_construction_area = (Spinner) findViewById(R.id.spinner_construction_area);
		spinner_in_out_length = (Spinner) findViewById(R.id.spinner_in_out_length);
		spinner_in_out_lc_type = (Spinner) findViewById(R.id.spinner_in_out_lc_type);
		spinner_in_out_value = (Spinner) findViewById(R.id.spinner_in_out_lc_value);
		spinner_in_in_lc = (Spinner) findViewById(R.id.spinner_in_in_lc);
		spinner_out_max_length_type = (Spinner) findViewById(R.id.spinner_out_max_length_type);
		spinner_out_max_length_value = (Spinner) findViewById(R.id.spinner_out_max_length_value);
		
		
		button_lengmei_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MobclickAgent.onEvent(context, "Account_calculator_utils");
				Intent intent = new Intent();
				intent.setClass(activity, RefrigerantCalculatorActivity.class);
				String barcode = ((TextView) findViewById(R.id.code_edit)).getText().toString();
				intent.putExtra("barcode", barcode);
				activity.startActivityForResult(intent, RefrigerantCalculatorActivity.REQUEST_CODE);
			}
		});
	}
	
	public void showView() {
		Layout_MutipleMachine.setVisibility(View.VISIBLE);
		initInOutMaxLength();
		initInOutLc();
		initInInLc();
		initFengGuanCD();
		initInstallation();
		initConstructionArea();
	}
	
	public void hideView(){
		Layout_MutipleMachine.setVisibility(View.GONE);
	}
	
	
	@Override
	public boolean validateView() {
		addCoolantWeight = edit_lengmei_add.getText().toString();
		diameter = edit_out_totle_line.getText().toString();
		if (addCoolantWeight==null||diameter==null||inDropHeight == null || tubeLength == null || dropHeight == null
				|| ioConnectionLength == null || installationSite == null
				|| constructionArea == null) {
			Utils.showToastCenter(activity,
					getString(R.string.barcode_info_is_incomplete));
			return false;
		}
		mutipleMachineEntity.setAddCoolantWeight(addCoolantWeight);
		mutipleMachineEntity.setDiameter(diameter);
		return true;
	}


	@Override
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer) throws Exception {
		HctResult result = new HctResult();
		result = controller.getBootPwd(paramEntity.phone, paramEntity.stripeCode,
				locationEntity.getmGpsLocation() ,
				locationEntity.getmBaseLocation(), locationEntity.getmWifiLocation(),locationEntity.getProvince_loc(),
				locationEntity.getCity_loc(), locationEntity.getProvince(), locationEntity.getCity(), locationEntity.getDistrict(), paramEntity.street,
				paramEntity.projectName, paramEntity.ownerName, paramEntity.ownerPhone, paramEntity.remark,
				paramEntity.industry, ioConnectionLength,
				inDropHeight, dropHeight, tubeLength,
				addCoolantWeight, diameter, "",
				installationSite, constructionArea, "",
				locationEntity.getmLatitude() + "",locationEntity.getmLongitude() + "", "", isClounServer);
		return result;
	}

	/**
	 * 关联提示页面用
	 * @param paramEntity
	 * @param locationEntity
	 * @param isClounServer
	 * @param mutipleMachineEntity
	 * @return
     * @throws Exception
     */
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer,MutipleMachineEntity mutipleMachineEntity) throws Exception {
		HctResult result = new HctResult();
		result = controller.getBootPwd(paramEntity.phone, paramEntity.stripeCode,
				locationEntity.getmGpsLocation() ,
				locationEntity.getmBaseLocation(), locationEntity.getmWifiLocation(),locationEntity.getProvince_loc(),
				locationEntity.getCity_loc(), locationEntity.getProvince(), locationEntity.getCity(), locationEntity.getDistrict(), paramEntity.street,
				paramEntity.projectName, paramEntity.ownerName, paramEntity.ownerPhone, paramEntity.remark,
				paramEntity.industry, mutipleMachineEntity.getIoConnectionLength(),
				mutipleMachineEntity.getInDropHeight(), mutipleMachineEntity.getDropHeight(), mutipleMachineEntity.getTubeLength(),
				mutipleMachineEntity.getAddCoolantWeight(), mutipleMachineEntity.getDiameter(), "",
				mutipleMachineEntity.getInstallationSite(), mutipleMachineEntity.getConstructionArea(), "",
				locationEntity.getmLatitude() + "",locationEntity.getmLongitude() + "", "", isClounServer);
		return result;
	}
	
	// 多联机安装位置
	private void initInstallation() {
		
		spinner_installation_site.setVisibility(View.VISIBLE);
		spinner_installation_site.setPrompt(getString(R.string.tag_select_hint));
		final String[] items = getResources().getStringArray(
				R.array.installation_site);
		installationSite = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_installation_site.setAdapter(adapter);

		spinner_installation_site.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					installationSite = null;
				} else {
					installationSite = items[arg2];
				}
				mutipleMachineEntity.setInstallationSite(installationSite);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	// 家用热水机建筑面积
	private void initConstructionArea() {
		
		final String[] items = getResources().getStringArray(
				R.array.construction_area);
		constructionArea = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_construction_area.setAdapter(adapter);

		spinner_construction_area.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					constructionArea = null;
				} else {
					constructionArea = items[arg2];
				}
				mutipleMachineEntity.setConstructionArea(constructionArea);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	/**
	 * 内你与外机的最大落差
	 */
	private void initInOutMaxLength() {
		
		final String[] items = getResources().getStringArray(
				R.array.in_out_length);
		ioConnectionLength = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_in_out_length.setAdapter(adapter);
		spinner_in_out_length.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					ioConnectionLength = null;
				} else {
					ioConnectionLength = items[arg2];
				}
				mutipleMachineEntity.setIoConnectionLength(ioConnectionLength);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	// 内外机最大落差
	private void initInOutLc() {
		final String[] items = getResources().getStringArray(
				R.array.in_out_lc_tag);
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_in_out_lc_type.setAdapter(adapter);

		
		spinner_in_out_value.setPrompt(getString(R.string.tag_select_hint));
		final String[] values = getResources().getStringArray(
				R.array.in_out_lc_value);
		dropHeight = null;
		SpinnerStringsAdapter vAdapter = new SpinnerStringsAdapter(activity, values);
		spinner_in_out_value.setAdapter(vAdapter);
		spinner_in_out_value.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					dropHeight = null;
				} else {
					dropHeight = values[arg2];
				}
				mutipleMachineEntity.setDropHeight(dropHeight);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		spinner_in_out_lc_type.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (position == items.length - 1) {
					dropHeight = items[position];
					spinner_in_out_value.setEnabled(false);
					spinner_in_out_value.setVisibility(View.INVISIBLE);
				} else if (position == 0) {
					dropHeight = null;
					spinner_in_out_value.setEnabled(false);
					spinner_in_out_value.setVisibility(View.INVISIBLE);
				} else {
					spinner_in_out_value.setEnabled(true);
					spinner_in_out_value.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	// 内内机最大落差
	private void initInInLc() {
		
		final String[] items = getResources().getStringArray(
				R.array.in_in_lc_value);
		inDropHeight = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_in_in_lc.setAdapter(adapter);

		spinner_in_in_lc.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					inDropHeight = null;
				} else {
					inDropHeight = items[arg2];
				}
				mutipleMachineEntity.setInDropHeight(inDropHeight);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	// 最大风管长度
	private void initFengGuanCD() {
		final String[] items = getResources().getStringArray(
				R.array.out_max_length_tag);
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_out_max_length_type.setAdapter(adapter);

		
		spinner_out_max_length_value.setPrompt(getString(R.string.tag_select_hint));
		final String[] values = getResources().getStringArray(
				R.array.out_max_length_value);
		tubeLength = null;
		final SpinnerStringsAdapter vAdapter = new SpinnerStringsAdapter(activity,
				values);
		spinner_out_max_length_value.setAdapter(vAdapter);
		spinner_out_max_length_value.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					tubeLength = null;
				} else {
					tubeLength = values[arg2];
				}
				mutipleMachineEntity.setTubeLength(tubeLength);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		spinner_out_max_length_type.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == items.length - 1) {
					tubeLength = items[arg2];
					spinner_out_max_length_value.setEnabled(false);
					spinner_out_max_length_value.setVisibility(View.INVISIBLE);
				} else if (arg2 == 0) {
					tubeLength = null;
					spinner_out_max_length_value.setEnabled(false);
					spinner_out_max_length_value.setVisibility(View.INVISIBLE);
				} else {
					spinner_out_max_length_value.setEnabled(true);
					spinner_out_max_length_value.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

}
